package com.zengde.chapter1.pojo;

import com.zengde.chapter1.pojo.def.Animal;
import com.zengde.chapter1.pojo.def.Person;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class BusinessPerson implements Person, BeanNameAware, BeanFactoryAware,
        ApplicationContextAware, InitializingBean, DisposableBean {

//    @Autowired
//    @Qualifier("dog")
    private Animal animal;

//    public BusinessPerson( Animal animal){
//        this.animal = animal;
//    }


    @Override
    public void service() {
        animal.use();
    }

    @Autowired @Qualifier("dog")
    @Override
    public void setAnimal(Animal dog) {
        System.out.println("延迟依赖注入");
        this.animal = dog;

    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("【" + this.getClass().getSimpleName()+ "】调用BeanFactoryAware的setBeanFactory()方法");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("【" + this.getClass().getSimpleName()+ "】调用BeanNameAware的setBeanName()方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("【" + this.getClass().getSimpleName()+ "】 DisposableBean()方法");
    }


    @PreDestroy
    public void destroy1() {
        System.out.println("【" + this.getClass().getSimpleName()+ "】注解@PreDestroy自定义的销毁方法");
    }


    @PostConstruct
    public void init() {
        System.out.println("【" + this.getClass().getSimpleName()+ "】注解@PostConstruct自定义的初始化方法");
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("【" + this.getClass().getSimpleName()+ "】调用InitializingBean的afterPropertiesSet()方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("【" + this.getClass().getSimpleName()+ "】调用ApplicationContextAware的setApplicationContext()方法");
    }


}
